<?php

namespace SL\BackBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use SL\FrontBundle\Form\PresseType;
use SL\FrontBundle\Entity\Presse;
use SL\FrontBundle\Entity\PresseRepository;

class PresseController extends Controller
{	
	public function indexPresseAction()
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Presse');
	
		$presses = $repository->getAllPresseWithThumbnail();

		return ($this->render('SLBackBundle:Presse:index.html.twig', array('presses' => $presses)));
	}
	
	public function savePresseAction()
	{
		$request = $this->getRequest();
	
		if ($request->getMethod() == 'POST')
		{
			$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Presse');
		
			$presses = $repository->findAll();

			foreach ($presses as $presse)
			{
				$projet->setPosition(intval($request->get(strval($presse->getId()))));
			}
			
			$em = $this->getDoctrine()->getManager();
			$em->flush();
			
		}
		return ($this->redirect($this->generateUrl('sl_back_presse_index')));
	}
	
	public function addPresseAction()
	{
		$presse = new Presse;
		
		$form = $this->createForm(new PresseType, $presse);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$presse->setUrl(str_replace(array(" ", ".", ",", "'", "\\", "/", "\"", "&", "é", "~", "$", "#", "|",
												  "è", "`", "ç", "à", "@", "¤", "µ", "ù", "¨", "^", "ë", "ä", "ü"), "-", $presse->getName()));
				$presse->setPosition(getLastPosition() + 1);

				$em = $this->getDoctrine()->getManager();
				$em->persist($projet);
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_back_presse_edit', array('url' => $presse->getUrl())));
			}
		}
				
		return $this->render('SLBackBundle:Presse:add.html.twig', 
									array('presse' => $presse, 'form' => $form->createView()));
	}
	
	public function deletePresseAction($url)
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Presse');
		
		$presse = $repository->findOneBy(array('url' => $url));
		if ($presse === null)
		{
			throw $this->createNotFoundException('Article '.$url.' inexistant.');
		}
		
		$form = $this->createFormBuilder()->getForm();
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$em = $this->getDoctrine()->getManager();
				$em->remove($presse);
				$em->flush();
				
				$this->get('session')->getFlashBag()->add('Administration', 'L\'article "'.$presse->getName().' a bien été supprimé');
				
				return $this->redirect($this->generateUrl('sl_back_presse_index'));
			}
		}
				
		return $this->render('SLBackBundle:Presse:delete.html.twig', 
									array('presse' => $presse, 'form' => $form->createView()));
	}
	
	public function editPresseAction($url)
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Presse');
		
		$presse = $repository->findOneBy(array('url' => $url));
		if ($presse === null)
		{
			throw $this->createNotFoundException('Article '.$url.' inexistant.');
		}
		
		$form = $this->createForm(new PresseType, $presse);
		
		$request = $this->get('request');
		if ($request->getMethod() == 'POST')
		{
			$form->bind($request);
			
			if ($form->isValid())
			{
				$em = $this->getDoctrine()->getManager();
				$em->flush();
				
				return $this->redirect($this->generateUrl('sl_back_presse_edit', array('url' => $url)));
			}
		}
				
		return $this->render('SLBackBundle:Presse:edit.html.twig', 
									array('presse' => $presse, 'form' => $form->createView()));
	}
	
	/* Private Methods */
	private function getLastPosition()
	{
		$repository = $this->getDoctrine()->getManager()->getRepository('SLFrontBundle:Presse');
		
		$presses = $repository->findAll();
		
		$lastpos = -1;
		
		foreach ($presses as $presse)
		{
			if ($lastpos == -1 || $lastpos < $presse->getPosition())
				$lastpos = $presse->getPosition();
		}
		return ($lastpos);
	}
}